package cn.larry.config;

import java.beans.PropertyEditor;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;

import cn.larry.demo.domain.Person;

/**
 * @author LarryZeal
 *
 */
@Configuration
public class BeanWrapperConfig {
	
	
	// 完全不知道beanWrapper()和bw()会有什么作用！！！--除了主动调用！
	@Bean
	public BeanWrapper beanWrapper() {
		BeanWrapper beanWrapper = new BeanWrapperImpl(); // 默认register default editors，不是注册到ApplicationContext！！！
		return beanWrapper;
	}

	@Autowired
	private Person person;

	@Bean
	public BeanWrapper bw() {
		// 工厂门面获取BeanWrapper实例。
		BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(person);
		wrapper.setPropertyValue("age", 100);
		System.out.println("from cn.larry.config.BeanWrapperConfig : " +person);
		return wrapper;
	}

	@Autowired(required = false)
	private List<PropertyEditor>			editors;	// 默认没有，因为不是Spring的东西，不在ApplicationContext中。
	@Autowired
	private Map<String, ApplicationContext>	map;

	@PostConstruct
	public void run() {
		if (editors != null) {
			for (PropertyEditor propertyEditor : editors) {
				System.out.println("默认注册的PropertyEditor：" + propertyEditor.getClass());
			}
		}
		System.out.println("-----------------------------------------");
		System.out.println(map.keySet());
	}

}
